package sk.activities;

import sk.model.Player;
import sk.model.Session;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class StatsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stats);

		Session session =
			new Session(getIntent().getStringExtra(
				SessionActivity.EXTRA_SESSION_FOR_STATS));

		buildTable(session);

	}

	private void buildTable(Session session) {
		LayoutParams params =
			new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT, 1);
		LinearLayout nameLayout =
			(LinearLayout) findViewById(R.id.stats_nameRow);
		for (int i = 0; i < session.getPlayers().size(); i++) {
			TextView textView = new TextView(nameLayout.getContext());
			textView.setGravity(Gravity.CENTER_HORIZONTAL);
			textView.setLayoutParams(params);
			textView.setText(session.getPlayers().get(i).getName());
			textView.setTextColor(Color.WHITE);
			nameLayout.addView(textView);
		}

		LinearLayout statsLayout =
			(LinearLayout) findViewById(R.id.stats_stats);
		for (int i = 0; i < session.getPlayers().size(); i++) {

			int sum = 0;
			Player p = session.getPlayers().get(i);
			LinearLayout column = new LinearLayout(statsLayout.getContext());
			column.setOrientation(LinearLayout.VERTICAL);
			column.setLayoutParams(params);
			column.setGravity(Gravity.CENTER);
			statsLayout.addView(column);

			for (int j = 0; j < session.getGames().size(); j++) {
				sum += session.getGames().get(j).getResult(p);
				TextView textView = new TextView(column.getContext());
				textView.setGravity(Gravity.CENTER);
				textView.setLayoutParams(params);
				textView.setText("" + sum);
				if (session.getGames().get(j).getResult(p) == 0) {
					textView.setText("-");
				}
				textView.setTextColor(Color.WHITE);
				column.addView(textView);
			}

		}

	}
}